"use strict";

var app = angular.module("App", ["ngRoute", "ngSanitize", "ngMaterial", "ngAnimate", "ui.bootstrap"]);

app.run(["$rootScope", "$location", "$window", "$templateCache", "$token", function ($rootScope, $location, $window, $templateCache, $token) {
    window.applicationCache.addEventListener("updateready", function (e) {
        if (window.applicationCache.status == window.applicationCache.UPDATEREADY) {
            $templateCache.removeAll();
            window.applicationCache.swapCache();
            window.applicationCache.update();
            window.location.reload();
        }
    }, false);

    $rootScope.$on("$routeChangeStart", function (event, next, current) {
        $token.get().then(function () {
            $rootScope.previousRoute = current;
        }, function () {
            if (next.originalPath == "/login") return;
            if (next.originalPath == "/visit") return;
            if (next.originalPath == "/visit/:q") return;
            if (next.originalPath == "/create") return;
            if (next.originalPath == "/shop/:login") return;
            $location.path("/login");
        });
    });

} ]);

app.config(["$routeProvider", "$locationProvider", "$mdThemingProvider", function ($routeProvider, $locationProvider, $mdThemingProvider) {
    $locationProvider.html5Mode = true;
    $routeProvider.
        when("/", {
            templateUrl: "/app/partials/app.html",
            controller: "AppCtrl"
        }).
        when("/login", {
            templateUrl: "/app/partials/login.html",
            controller: "LoginCtrl"
        }).
        when("/search/:q", {
            templateUrl: "/app/partials/search-results.html",
            controller: "SearchResultCtrl"
        }).
        when("/search", {
            templateUrl: "/app/partials/search.html",
            controller: "SearchCtrl"
        }).
        when("/account", {
            templateUrl: "/app/partials/account.html",
            controller: "AccountCtrl"
        }).
        when("/parameters", {
            templateUrl: "/app/partials/parameters.html",
            controller: "AccountCtrl"
        }).
        when("/subscriptions", {
            templateUrl: "/app/partials/subscriptions.html",
            controller: "SubscriptionsCtrl"
        }).
        when("/shop/:login", {
            templateUrl: "/app/partials/shop.html",
            controller: "ShopCtrl"
        }).
        when("/alert/:id", {
            templateUrl: "/app/partials/alert.html",
            controller: "AlertCtrl"
        }).
        when("/visit/:q", {
            templateUrl: "/app/partials/visit-results.html",
            controller: "VisitResultCtrl"
        }).
        when("/visit", {
            templateUrl: "/app/partials/visit.html",
            controller: "VisitCtrl"
        }).
        when("/create", {
            templateUrl: "/app/partials/create.html",
            controller: "UserCreatorCtrl"
        }).
        otherwise({
            redirectTo: '/'
        });

    
    $mdThemingProvider.definePalette('blackPink', $mdThemingProvider.extendPalette('grey', {
        '0': '#e91e63',
        '100': '#ec407a',
        '500': '#212121',
    }));

    $mdThemingProvider.theme('default')
        .primaryPalette('blackPink', {
            'default': '900'
        })
        .accentPalette('pink')
        .warnPalette('lime')
        .backgroundPalette('grey', {
            'default': '800'
        });

} ]);

app.controller("AppCtrl", ["$scope", "$mdSidenav", "$rootScope", "appAccount", function ($scope, $mdSidenav, $rootScope, appAccount) {
    if (!$rootScope.previousRoute) $scope.viewClass = "app";
    else $scope.viewClass = "app";

    $scope.selectedIndex = 0;

    $scope.next = function () {
        $scope.selectedIndex = Math.min($scope.selectedIndex + 1, 2);
    };

    $scope.previous = function () {
        $scope.selectedIndex = Math.max($scope.selectedIndex - 1, 0);
    };

    $scope.account = appAccount;
    $scope.account.init()

    $scope.openMenu = function () {
        $mdSidenav('left').toggle();
    };

} ]);

app.controller("LoginCtrl", ["$scope", "$mdSidenav", "connect", function ($scope, $mdSidenav, connect) {
    $scope.pageClass = "login";
    $scope.model = {
        login: "",
        password: ""
    };

    $scope.validate = function () {
        return connect.connect($scope.model);
    };

    $scope.openMenu = function () {
        $mdSidenav('left').toggle();
    };
} ]);

app.controller("AccountCtrl", ["$window", "$mdSidenav", "$scope", "googleGeocode", "appAccount", function ($window, $mdSidenav, $scope, googleGeocode, appAccount) {
    $scope.viewClass = "account";

    $scope.account = appAccount;
    $scope.account.init().then(function(){
        $scope.locationText = $scope.account.data.location.address;
    });

    $scope.back = function () {
        $window.history.back();
    };

    $scope.getLocation = function (val) {
        return googleGeocode.getLocations(val);
    };

    $scope.openMenu = function () {
        $mdSidenav('left').toggle();
    };
} ]);

app.controller("SubscriptionsCtrl", ["$window", "$mdSidenav", "$scope", "appSubscription", "appNews", "appAccount", function ($window, $mdSidenav, $scope, appSubscription, appNews, appAccount) {
    
    $scope.viewClass = "subscriptions";

    $scope.account = appAccount;
    $scope.account.init();

    $scope.list = appSubscription;
    $scope.list.init();
        
    $scope.back = function(){
        $window.history.back();
    };

    $scope.openMenu = function () {
        $mdSidenav('left').toggle();
    };
} ]);

app.controller("HomeCtrl", ["$scope", "appHome", function ($scope, appHome) {
    $scope.pageClass = "home";

    $scope.list = appHome;
    $scope.list.init();
} ]);

app.controller("NewsCtrl", ["$scope", "appNews", function ($scope, appNews) {
    $scope.pageClass = "news";

    $scope.list = appNews;
    $scope.list.init();
} ]);

app.controller("LikeCtrl", ["$scope", "appLike", function ($scope, appLike) {
    $scope.pageClass = "like";

    $scope.list = appLike;
    $scope.list.init();
} ]);

app.controller("SearchCtrl", ["$scope", "$mdSidenav", "$window", "appAccount", "appSearch", function ($scope, $mdSidenav, $window, appAccount, appSearch) {
    $scope.viewClass = "search";

    $scope.account = appAccount;
    appAccount.init();

    $scope.list = appSearch;
    $scope.list.init("");

    $scope.back = function () {
        $window.history.back();
    };

    $scope.openMenu = function () {
        $mdSidenav('left').toggle();
    };
} ]);

app.controller("SearchResultCtrl", ["$route", "$mdSidenav", "$window", "$scope", "appAccount", "appSearch", function ($route, $mdSidenav, $window, $scope, appAccount, appSearch) {
    $scope.viewClass = "search";

    $scope.account = appAccount;
    appAccount.init();

    $scope.list = appSearch;
    $scope.list.init($route.current.params.q);

    $scope.back = function () {
        $window.history.back();
    };

    $scope.openMenu = function () {
        $mdSidenav('left').toggle();
    };
} ]);

app.controller("VisitCtrl", ["$scope", "$mdSidenav", "$window", "appSearch", function ($scope, $mdSidenav, $window, appSearch) {
    $scope.viewClass = "visit";

    $scope.list = appSearch;
    $scope.list.init("");

    $scope.back = function () {
        $window.history.back();
    };

    $scope.openMenu = function () {
        $mdSidenav('left').toggle();
    };
} ]);

app.controller("VisitResultCtrl", ["$route", "$mdSidenav", "$window", "$scope", "appSearch", function ($route, $mdSidenav, $window, $scope, appSearch) {
    $scope.viewClass = "search";

    $scope.list = appSearch;
    $scope.list.init($route.current.params.q);

    $scope.back = function () {
        $window.history.back();
    };

    $scope.openMenu = function () {
        $mdSidenav('left').toggle();
    };
} ]);


app.filter("unsafe", function($sce) {
    return function(val) {
        return $sce.trustAsHtml(val);
    };
});

app.controller("AlertCtrl", ["$route", "$mdSidenav", "$scope", "$window", "appAlert", "appAccount", function ($route, $mdSidenav, $scope, $window, appAlert, appAccount) {
    $scope.viewClass = "alert";

    var alertId = $route.current.params.id;

    $scope.account = appAccount;
    $scope.account.init();

    $scope.alert = appAlert.get(alertId);
    $scope.alert.read = true;
    $scope.alert.init();

    $scope.back = function () {
        $window.history.back();
    };

    $scope.openMenu = function () {
        $mdSidenav('left').toggle();
    };
} ]);

app.controller("ShopCtrl", ["$scope", "$mdSidenav", "$route", "$window", "appShop", "appShopAlerts", "appAccount", function ($scope, $mdSidenav, $route, $window, appShop, appShopAlerts, appAccount) {
    $scope.viewClass = "shop";

    var login = $route.current.params.login;

    $scope.account = appAccount;
    $scope.account.init();

    $scope.shop = appShop.get(login);
    $scope.shop.init();

    $scope.shopAlerts = appShopAlerts.get(login);
    $scope.shopAlerts.init();

    $scope.back = function () {
        $window.history.back();
    };

    $scope.openMenu = function () {
        $mdSidenav('left').toggle();
    };
} ]);

app.controller("ShopMapCtrl", ["$scope", "$mdSidenav", "$route", "$window", "googlemap", "appShop", "appAccount", function ($scope, $mdSidenav, $route, $window, googlemap, appShop, appAccount) {
    $scope.viewClass = "map";

    var login = $route.current.params.login;
    var map = document.getElementById("map-js");
    
    $scope.account = appAccount;
    $scope.account.init();
    
    $scope.shop = appShop.get(login);
    $scope.shop.init();

    googlemap.init(map, $scope.shop.data.location.loc.coordinates[0], $scope.shop.data.location.loc.coordinates[1], 8).then(function(){
        googlemap.addShop($scope.shop.data);
    });

    $scope.back = function(){
        $window.history.back();
    };

    $scope.openMenu = function () {
        $mdSidenav('left').toggle();
    };
} ]);

app.controller("UserCreatorCtrl", ["$window", "$mdSidenav", "$scope", "googleGeocode", "appUserCreator", function ($window, $mdSidenav, $scope, googleGeocode, appUserCreator) {
    $scope.viewClass = "user";

    $scope.user = appUserCreator;
    $scope.user.init();

    $scope.back = function () {
        $window.history.back();
    };

    $scope.getLocation = function (val) {
        return googleGeocode.getLocations(val);
    };

    $scope.openMenu = function () {
        $mdSidenav('left').toggle();
    };

} ]);
